package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.*;
import com.itheima.service.ClazzService;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
    @Autowired
    private ClazzService clazzService;
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public PageResult pageByCondition(StudentQueryParam student) {
        Page<Student> studentPage = new Page<>(student.getPage(), student.getPageSize());
        LambdaQueryWrapper<Student> lqw = new LambdaQueryWrapper<>();
        lqw.like(student.getName() != null && student.getName().length() > 0, Student::getName, student.getName());
        lqw.eq(student.getDegree() != null, Student::getDegree, student.getDegree());
        lqw.eq(student.getClazzId() != null, Student::getClazzId, student.getClazzId());
        //查询出的学生信息
        studentPage = this.page(studentPage, lqw);
        //查询班级名称

        List<Student> records = studentPage.getRecords();
        Set<Integer> collect = records.stream().map(stu -> stu.getClazzId()).filter(Objects::nonNull).collect(Collectors.toSet());
        //判断不为null，就查询
        if (collect != null && collect.size() > 0) {
            List<Clazz> clazzes = clazzService.listByIds(collect);
            //将其转换为map集合
            Map<Integer, String> clazzIdAndName = clazzes.stream().collect(Collectors.toMap(Clazz::getId, Clazz::getName));
            records.forEach(s -> s.setClazzName(clazzIdAndName.get(s.getClazzId())));
        }
        return new PageResult(studentPage.getTotal(), records);
    }

    @Override
    public void saveStudent(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        this.save(student);
    }

    @Override
    public Student selectStudentById(Integer id) {
        return studentMapper.selectById(id);
    }

    @Override
    public void updateStudent(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.updateById(student);
    }

    @Override
    public void violation(Integer id, Integer score) {
        //学生违纪
        //sql:update Student set ,update_time =? where id =?;
        LambdaUpdateWrapper<Student> studentLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        studentLambdaUpdateWrapper.eq(Student::getId, id);
        studentLambdaUpdateWrapper.set(Student::getUpdateTime, LocalDateTime.now());
        studentLambdaUpdateWrapper.setSql("violation_count=violation_count+{0}", 1);
        studentLambdaUpdateWrapper.setSql(score != null, "violation_score=violation_score+{0}", score);
        studentMapper.update(studentLambdaUpdateWrapper);
    }
}
